package com.paipaideli.ui;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.utils.preferrnces.FastData;
import com.paipaideli.ui.login.LoginActivity;
import com.paipaideli.ui.webView.WebAcyivity;

import java.util.ArrayList;

/**
 * 引导界面
 * bug1
 * test
 * wangweicommit
 */
@SuppressLint("SdCardPath")
public class GuidePagesActivity extends BasePresenterActivity {
    /**
     * Viewpager对象
     */
    private ViewPager viewPager;
    private ImageView imageView;
    /**
     * 创建一个数组，用来存放每个页面要显示的View
     */
    private ArrayList<View> pageViews;
    /**
     * 创建一个imageview类型的数组，用来表示导航小圆点
     */
    private ImageView[] imageViews;
    /**
     * 装显示图片的viewgroup
     */
    private ViewGroup viewPictures;
    /**
     * 导航小圆点的viewgroup
     */
    private ViewGroup viewPoints;
    private static final String SHAREDPREFERENCES_NAME = "first_pref";
    private RelativeLayout relativeLayout_btn;

    @Override
    public int getContentView() {
        return R.layout.guidepage_viewpagers;
    }

    @Override
    public void initView() {
        //安卓6.0会变化的权限
        //可以将一下代码加到你的MainActivity中，或者在任意一个需要调用分享功能的activity当中
        String[] mPermissionList = new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CAMERA
        };
        ActivityCompat.requestPermissions(GuidePagesActivity.this, mPermissionList, 100);

        LayoutInflater inflater = getLayoutInflater();
        pageViews = new ArrayList<View>();
        //添加引导图片
        pageViews.add(inflater.inflate(R.layout.guidepage_one, null));
        pageViews.add(inflater.inflate(R.layout.guidepage_two, null));
        pageViews.add(inflater.inflate(R.layout.guidepage_three, null));
        pageViews.add(inflater.inflate(R.layout.guidepage_four, null));

        // 小圆点数组，大小是图片的个数
        imageViews = new ImageView[pageViews.size()];
        // 从指定的XML文件中加载视图
        viewPictures = (ViewGroup) inflater.inflate(R.layout.guidepage_viewpagers, null);
        viewPager = viewPictures.findViewById(R.id.guidePagers);
        viewPoints = viewPictures.findViewById(R.id.viewPoints);
        relativeLayout_btn = viewPictures.findViewById(R.id.relativeLayout_btn);

        // 添加小圆点导航的图片
        for (int i = 0; i < pageViews.size(); i++) {
            imageView = new ImageView(GuidePagesActivity.this);
            imageView.setLayoutParams(new LayoutParams(40, 40));
            imageView.setPadding(8, 5, 8, 10);
            // 吧小圆点放进数组中
            imageViews[i] = imageView;
            // 默认选中的是第一张图片，此时第一个小圆点是选中状态，其他不是
            if (i == 0)
                imageViews[i].setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator_focused));
            else
                imageViews[i].setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator_unfocused));
            // 将imageviews添加到小圆点视图组
            viewPoints.addView(imageViews[i]);
        }
        setContentView(viewPictures);
        viewPager.setAdapter(new NavigationPageAdapter());
        // 为viewpager添加监听，当view发生变化时的响应
        viewPager.setOnPageChangeListener(new NavigationPageChangeListener());
    }

    // 导航图片view的适配器，必须要实现的是下面四个方法
    class NavigationPageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        // 初始化每个Item
        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(pageViews.get(position));
            return pageViews.get(position);
        }

        // 销毁每个Item
        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(pageViews.get(position));
        }

    }

    // viewpager的监听器，主要是onPageSelected要实现
    class NavigationPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            // 循环主要是控制导航中每个小圆点的状态
            for (int i = 0; i < imageViews.length; i++) {
                // 当前view下设置小圆点为选中状态
                imageViews[i].setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator_focused));
                // 其余设置为非选中状态
                if (position != i)
                    imageViews[i].setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator_unfocused));
            }
            if ((position + 1) == imageViews.length) {
                relativeLayout_btn.setVisibility(View.GONE);
            } else {
                relativeLayout_btn.setVisibility(View.VISIBLE);
            }
        }

    }

    // 开始按钮方法，开始按钮在XML文件中onClick属性设置；
    // 我试图把按钮在本activity中实例化并设置点击监听，但总是报错，使用这个方法后没有报错，原因没找到
    public void startbutton(View v) {
        FastData.setFirstInApp(true);
//        if (FastData.isLogin()) {
            Intent intent = new Intent(GuidePagesActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
//        } else {
//            LoginActivity.open(getCurrentActivity());
//        }
    }
}
